//
//  HomeProditemHCell.m
//  JiaoX
//
//  Created by yinqy on 15/1/30.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "HomeProditemHCell.h"



@interface HomeProditemHCell()
{
    UIImageView *_prodImage;
    UILabel *_prodName;
    UILabel *_nowPrice;
    UILabel *_oldPrice;
    UILabel *_buyCount;
}
@end
@implementation HomeProditemHCell

+ (instancetype)cellWithCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath{
    HomeProditemHCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HOME_PROD_IDENTIFIER forIndexPath:indexPath];
    return cell;
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        //添加一个imageView
        UIImageView *prodImage = [[UIImageView alloc]init];
        CGFloat width = (JXScreenW-3*JXPublicInset)*0.5;
        prodImage.frame = CGRectMake(5, 5, width-10, width-10);
        _prodImage = prodImage;
        [self addSubview:prodImage];
        
        //添加商品名字
        UILabel *prodName = [[UILabel alloc]init];
        prodName.font = ProdNameFont;
        prodName.numberOfLines  =1;
        prodName.frame = CGRectMake(5, CGRectGetMaxY(prodImage.frame)+JXPublicInset, width-5, prodName.font.lineHeight*2);
        prodName.numberOfLines = 2;
        _prodName = prodName;
        [self addSubview:prodName];
        
        
        //添加商品现价
        UILabel *nowPrice = [[UILabel alloc]init];
        nowPrice.font = ProdNewPriceFont;
        nowPrice.textColor = JXThemeColor;
        _nowPrice = nowPrice;
        [self addSubview:nowPrice];
        
        //添加商品原价
        UILabel *oldPrice = [[UILabel alloc]init];
        oldPrice.font = ProdBuyCountFont;
        oldPrice.textColor = JXColor(153, 153, 153);
        _oldPrice = oldPrice;
        [self addSubview:oldPrice];
        
        //添加讨论人数
        UILabel *buyCount = [[UILabel alloc]init];
        buyCount.font = ProdBuyCountFont;
        buyCount.textColor = JXColor(153, 153, 153);
        _buyCount = buyCount;
        [self addSubview:buyCount];
        
        self.backgroundColor = [UIColor whiteColor];
        
        //设置layer
        CALayer *layer=[self layer];
        //设置边框线的宽
        [layer setBorderWidth:1];
        //设置边框线的颜色
        [layer setBorderColor:[JXColor(242, 242, 242) CGColor]];
    }
    return self;
}

-(void)setItemInfo:(ProductItemInfo *)itemInfo{
    _itemInfo = itemInfo;
    
    //设置商品图片
    [_prodImage sd_setImageWithURL:[NSURL URLWithString:itemInfo.prodImage]];
    //设置商品名称
    [_prodName setText:itemInfo.prodWholeName];
    //设置商品价格
    [_nowPrice setText:itemInfo.prodNewPrice];
    CGSize nowPriceSize = [itemInfo.prodNewPrice sizeWithFont:ProdNewPriceFont];
    _nowPrice.frame =CGRectMake(5, CGRectGetMaxY(_prodName.frame)+JXPublicInset, nowPriceSize.width, nowPriceSize.height);
    
    //设置商品旧价格
    [_oldPrice setText:itemInfo.prodOldPrice];
    CGSize oldPriceSize = [itemInfo.prodOldPrice sizeWithFont:ProdBuyCountFont];
    _oldPrice.frame = CGRectMake(CGRectGetMaxX(_nowPrice.frame)+5, CGRectGetMaxY(_nowPrice.frame)-oldPriceSize.height, oldPriceSize.width, oldPriceSize.height);
    
    //设置商品购买人数
    [_buyCount setText:itemInfo.prodBuyCount];
    CGSize buyCountSize =[itemInfo.prodBuyCount sizeWithFont:ProdBuyCountFont];
    CGFloat comtCountX = self.width-buyCountSize.width-JXPublicInset/2;
    CGFloat comtCountY = CGRectGetMaxY(_nowPrice.frame)-buyCountSize.height;
    _buyCount.frame = CGRectMake(comtCountX, comtCountY, self.width-JXPublicInset/2-comtCountX, buyCountSize.height);
    [self setBounds:CGRectMake(0, 0, (JXScreenW-JXPublicInset*3)/2, HomeProditemHCell_HEIGHT)];
}
@end
